
<%@ include file="/common/taglibs.jsp"%>
<!--分页查询共用的页面-->
<%@ include file="/common/common.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>
	
<!--引用的标签页js库-->
<script type="text/javascript" src="<%=jsPath%>/jquery/jquery-asTabs.min.js"></script>
<link rel="stylesheet" type="text/css" href="<%=jsPath%>/jquery/css/jquery-asTabs.css" />

<script type="text/javascript" src="<%=jsPath%>/jquery/jquery.timers.js"></script><!--定时器-->
<script type="text/javascript" src="<%=jsPath%>/terminalCommand.js?v=34"></script><!--终端命令结果查询-->
<script>

	/*
	修改代码：
	1.新定义变量TerminalCommmand，为命令支持变量
	2.重定义id：
	原：null		现：avParamCommandMsg
	原：null		现：wmBtnQuery
	原：		现：
	*/

	var tc2AvParam = new TerminalCommand2();
    var timerName = "avparamTimer";
    ///////////////////////////////////////////////////////////////
    tc2AvParam.messageDiv = $("#avParamCommandMsg");
	tc2AvParam.timerName = timerName;
	//tc2WakeupMode.commandButton = $("#wmBtnQuery");


//var timerName = "timerForQuery";
var operation = "";//操作类型是查询query还是修改modify
var times=30;


//当查询终端参数命令执行成功时
function onQueryCommandSuccess(result){
    if (result.success) {
        var p = result.data[0];
        var strArray = p.value.split(",");
        var i = 0;
        Utility.setComboboxDefaultValue("realTimeCodecMode", strArray[i++]);
        Utility.setComboboxDefaultValue("resolution", strArray[i++]);
        $("#keyFrameInterval").val(strArray[i++]);
        $("#frameRate").val(strArray[i++]);
        $("#bitRate").val(strArray[i++]);
        Utility.setComboboxDefaultValue("storageCodecMode", strArray[i++]);
        Utility.setComboboxDefaultValue("storageResolution", strArray[i++]);
        $("#storageKeyFrameInterval").val(strArray[i++]);
        $("#storageFrameRate").val(strArray[i++]);
        $("#storageBitRate").val(strArray[i++]);
        var osdValue = parseInt(strArray[i++]);
        var str = osdValue.toString(2);
        var strOsdArray = str.split("");
        var osdArray = [];
        $.each(strOsdArray, function(i,v)
		{
		    if(v == "1")
		        osdArray.push(i);
		})
		var osdText = osdArray.join(",");
        $("#osdText").val(osdText);//修改隐含参数
        $("#osdCombo").combotree('setValues',osdArray);//设置下拉框
        Utility.setComboboxDefaultValue("enableAudio", strArray[i++]);
    }

}

/**
 * 下发参数查询指令
 */
function sendParamQueryCommand()
{
    operation = "query";
    var url = "<%=ApplicationPath%>/videoTerminalParam/sendParamQueryCommand.action";
    var param = {paramId : 0x0075,vehicleId:${vehicleId}};
    $.getJSON(url, param, function(result)
    {
        if (result.success) {
            var commandId = result.data; //下发成功后，获取到命令Id
            tc2AvParam.commandButton = $("#avParamBtnQuery");
            var url = "<%=ApplicationPath%>/terminalParam/queryTerminalParam.action";
            tc2AvParam.times=190;
            tc2AvParam.startQueryResult(commandId,url,onQueryCommandSuccess);//命令下发成功,根据命令id,开始尝试获取检索结果

        }else {
            $("#avParamCommandMsg").html("提交失败! 错误原因：" + (result.message ? result.message : result.Data));
            //停止所有的在$('body')上定时器
            $('#avParamBody').stopTime (timerName);
        }
    });
}


 function startQueryResult(commandId)
{ 
	times=60;
	$('#avParamBtnQuery').attr("disabled","disabled");
	$("#avParamBtnQuery").val("等待终端返回查询结果("+times+" )");
	$('#avParamBody').oneTime('500ms', timerName,function(){  
		 queryResult(commandId); //查询命令的返回结果
	});
}
//根据下发参数查询命令ID，来查询命令返回的参数结果
function queryResult(commandId)
{	
    times--;
	$('#avParamBtnQuery').attr("disabled","disabled");
	$("#avParamBtnQuery").val("等待终端返回查询结果("+times+" )");
	

	//alert(times);
       var url = "<%=ApplicationPath%>/takePicture/getPictureConfig.action";
	  //var commandId = 0;
	  var params = {commandId:commandId,vehicleId:${vehicleId}};
	   $.getJSON(url, params, 
			function(result){		
				  if(result.success == true)
				 {
					         $("#avParamCommandMsg").html('查询成功');
								//返回的查询结果，填充到input输入框中
							  if(console.log)
								  console.log(result.message);
							  setPhotoConfig(result.message);//获取参数后，填充到表单中
							 $("#avParamBtnQuery").val("参数查询");
							$('#avParamBtnQuery').attr("disabled",false);		  

					  
					 //停止所有的在$('body')上定时器  
					 $('#avParamBody').stopTime (timerName);   
					//$('#btnQuery').removeAttr("disabled");
				}else {
					if(times == 1)
				   {
						 $("#avParamCommandMsg").html("命令执行超时，没有返回结果,请重新查询");
						 $("#avParamBtnQuery").val("参数查询");
							$('#avParamBtnQuery').attr("disabled",false);
					    $('#avParamBody').stopTime (timerName);  
				   }else
					{
					   $("#avParamCommandMsg").html(result.message);
					     //最多执行10次
						 $('#avParamBody').oneTime('900ms', timerName,function(){  
								 queryResult(commandId); //查询命令的返回结果
						});
					}
					
				}
		  }
	  );
}

//当终端命令执行成功时
function onCommandSuccess(result){		
	var data = result.data;
					 $("#avParamCommandMsg").html(data.status);
					 if(data.fileName )
					 {
				          var picturePath = "<%=ApplicationPath%>/vehiclePicture/"; 
						  picturePath += data.fileName;
						 $("#imgPic").attr("src", picturePath);
						 var html = "<a href='"+ picturePath + "'>下载文件</a>";
						 $("#td_ViewFile").html(html);
					 }
}

function onOsdComboTreeChecked(selectedValues)
{
    $("#osdText").val(selectedValues);
}

$().ready(function() {
	 $("#avParamEntityForm").validate(); //初始化验证信息
	//ajax填充下拉框数据 填充监听类型 选项
	 $("#osdCombo").initComboTree({params:{category:"OsdText"},onCheck:onOsdComboTreeChecked,selectedValues:[0,1,2]});
	Utility.ajaxSubmitForm("avParamEntityForm", {
						 success:function(responseText)
						 {
							   var result = responseText;
							   tc2AvParam.commandButton = $("#avParamBtnConfig");
							     var saveType = $("#saveType").val();
								 tc2AvParam.messageDiv = $("#avParamCommandMsg");
								if (result.success) {
										 var commandId = result.data; //下发成功后，获取到命令Id
									 if(saveType == 0)
									 {
										 var url = "<%=ApplicationPath%>/takePicture/getPictureResult.action";
										 tc2AvParam.times=100;
										 tc2AvParam.commandButton = $("#avParamBtnConfig");
										 tc2AvParam.startQueryResult(commandId,url,onCommandSuccess);//命令下发成功,根据命令id,开始尝试获取检索结果
									  }else
									 {
									 //如果类型是保存到终端，则不需要检测上传
									 tc2AvParam.startQueryResult(commandId);
									 }
								}
								else {
									$("#avParamCommandMsg").html("提交失败! 错误原因：" + (result.message ? result.message : result.Data));
									//$('body').stopTime ();  
								 }
						 }
				 });

		    $("#avParamBtnQuery").click(function()
			 {
                 sendParamQueryCommand();//下发参数查询命令
			 });


});
var hide = true;
function showAdvanced()
{
	if(hide)
	{
		$(".advanced").show();
	}else
		$(".advanced").hide();
	hide = hide != true;
}

 
</script>
 <BODY id="avParamBody">
	<form id="avParamEntityForm" name="avParamEntityForm" 
			action='<%=ApplicationPath%>/videoTerminalParam/configAvParam.action' method="POST">
				
        <input type="hidden"  name="vehicleId"  id="vehicleId" value="${vehicleId}"/>
		<input type="hidden"  name="osdText"  id="osdText" value="0,1,2"/>
  <table width="100%"  class="TableBlock">
					<tbody><tr>
						<td colspan="4" style="font-weight: bold; background: #EFEFEF;" height="25">音视频参数设置

							<button id="avParamBtnConfig" type="submit" class="button green medium sendjson" ><i class="fa fa-cog" style="margin-right:3px;"></i> 设置参数</button>

							<button id="avParamBtnQuery" type="button" class="button blue medium " ><i class="fa fa-reply-all" style="margin-right:3px;"></i> 查询参数</button>
						<span id="avParamCommandMsg" class="commandMsg"></span>
						</td>
					</tr>
					<tr>
						<td align="right">实时流编码模式:</td>
						<td >
						    <select id="realTimeCodecMode" name="realTimeCodecMode">
						    <option value="0">CBR(固定码率)</option>
						    <option value="1">VBR(可变码率)</option>
						    <option value="2">ABR(平均码率)</option>
							</select>
						</td>
						<td align="right">实时流分辨率:</td>
						<td >
						    <select id="resolution" name="resolution">
								<option value="0">QCIF</option>
						    <option value="1">CIF</option>
						    <option value="2">WCIF</option>
						    <option value="3">DI</option>
						    <option value="4">WDI</option>
						    <option value="5">720P</option>
						    <option value="6">1080P</option>
							</select>
						</td>
					</tr>
					
					<tr>
						
						<td align="right">实时流关键帧间隔(帧)
							:</td>
						<td>
						<input type="text" id="keyFrameInterval"   name="keyFrameInterval" maxlength="16" size="20"  value="10"
						   class="required digits min:1" style="width:80px">
						  </td>
						<td align="right">实时流目标帧率(帧/s):</td>
						<td ><input id="frameRate" name="frameRate" class="required digits"  value="10" maxlength="16" size="20" style="width:100px"  class="required digits">
						</td>
					</tr>
					
					<tr>
						<td align="right">实时流目标码率(kbps):</td>
						<td >
							<input type="text" id="bitRate"   name="bitRate" maxlength="16" size="20"  value="10"
								   class="required digits min:1" style="width:80px">

						</td>
						<td align="right">存储流编码模式:</td>
						<td >
							<select id="storageCodecMode" name="storageCodecMode">
								<option value="0">CBR(固定码率)</option>
								<option value="1">VBR(可变码率)</option>
								<option value="2">ABR(平均码率)</option>
							</select>
						</td>

					</tr>
					
					<tr class="advanced">

						<td align="right">存储流分辨率:</td>
						<td >
							<select id="storageResolution" name="storageResolution">
								<option value="0">QCIF</option>
								<option value="1">CIF</option>
								<option value="2">WCIF</option>
								<option value="3">DI</option>
								<option value="4">WDI</option>
								<option value="5">720P</option>
								<option value="6">1080P</option>
							</select>
						</td>
						<td align="right">存储流关键帧间隔(帧)
						:</td>
						<td>
							<input type="text" id="storageKeyFrameInterval"   name="storageKeyFrameInterval" maxlength="16" size="20"  value="10"
								   class="required digits min:1" style="width:80px">
						</td>
					</tr>
				
					<tr  class="advanced">
						<td align="right">存储流目标帧率(帧/s):</td>
						<td ><input id="storageFrameRate" name="storageFrameRate" class="required digits"  value="10" maxlength="16" size="20" style="width:100px"  class="required digits">
						</td>
						<td align="right">存储流目标码率(kbps):</td>
						<td >
							<input type="text" id="storageBitRate"   name="storageBitRate" maxlength="16" size="20"  value="10"
								   class="required digits min:1" style="width:80px">

						</td>
					</tr>
					
					<tr  class="advanced">
						<td align="right">OSD字幕叠加设置:</td>
						<td >
							<select id="osdCombo" class="easyui-combotree" style="width: 200px; height: 24px;">
							</select>
						</td>
						<td align="right">音频输出:</td>
						<td >
							<select id="enableAudio" name="enableAudio">
								<option value="0">不启用</option>
								<option value="1">启用</option>
							</select>
						</td>
					</tr>

					
				</tbody></table>
				</form>

 </BODY>
</HTML>
